<template>
  <div class="app-container">
    <el-card>
      <el-row>
        <el-form ref="queryForm" inline size="small" :model="queryForm">
          <el-form-item label="所属楼栋" prop="buildId">
            <el-select v-model="queryForm.buildId" placeholder="请选择所属楼栋" filterable>
              <el-option
                v-for="item in buildList"
                :key="item.id"
                :label="item.label"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="教室名" prop="name">
            <el-input placeholder="请输入教室名" v-model="queryForm.name"></el-input>
          </el-form-item>
          <el-button icon="el-icon-search" size="small" type="primary" @click="onClickSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQueryForm">重置</el-button>
        </el-form>
      </el-row>
      <el-row>
        <el-button icon="el-icon-plus" size="small" type="primary" plain @click="onClickAdd">新增</el-button>
        <el-button icon="el-icon-edit" size="small" type="success" plain @click="onClickEdit()"
                   :disabled="editDisabled">
          修改
        </el-button>
        <el-button icon="el-icon-edit" size="small" type="danger" plain>删除</el-button>
      </el-row>
    </el-card>
    <el-table
      v-loading="loading"
      :data="list"
      element-loading-text="加载中"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column align="center" label="ID" width="95" prop="id"></el-table-column>
      <el-table-column label="所属楼栋" prop="build.name"></el-table-column>
      <el-table-column label="教室名" prop="name"></el-table-column>
      <el-table-column label="可容纳人数" prop="number"></el-table-column>
      <el-table-column label="当前状态" prop="status">
        <template v-slot="scope">
          <status-show data-name="classroom_status" :value="scope.row.status"></status-show>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <el-drawer
      :title="drawer.title"
      :visible.sync="drawer.show"
      :before-close="onDrawerClose"
      size="50%">
      <el-form :rules="formRules" ref="editForm" label-width="auto" :model="form" size="medium">
        <el-form-item label="所属楼栋" prop="buildId">
          <el-select v-model="form.buildId" placeholder="请选择所属楼栋" filterable>
            <el-option
              v-for="item in buildList"
              :key="item.id"
              :label="item.label"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教室名" prop="name">
          <el-input v-model="form.name" placeholder="请输入教室名"></el-input>
        </el-form-item>
        <el-form-item label="可容纳人数" prop="number">
          <el-input v-model.number="form.number" placeholder="请输入可容纳人数"></el-input>
        </el-form-item>
        <el-form-item label="当前状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="item.value" v-for="(item, index) in enums.classroom_status">{{ item.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div class="form-drawer-footer">
        <el-button @click="onDrawerClose">取 消</el-button>
        <el-button type="primary" @click="onDrawerConfirm" :loading="drawer.confirmLoading">{{ drawer.confirmLoading ? '提交中 ...' : '确 定' }}</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {addClassroom, deleteOneClassroom, getClassroomList, getOneClassroom, updateClassroom} from "@/api/classroom";
import List from "@/mixins/list";
import {getBuildSimpleList} from "@/api/build";
import ClassroomValidator from "@/validator/ClassroomValidator";
import {mapGetters, mapState} from "vuex";
import StatusShow from "@/components/StatusShow";

export default {
  components: {StatusShow},
  data() {
    return {
      queryListFun: getClassroomList,
      addFun: addClassroom,
      updateFun: updateClassroom,
      queryOneFun: getOneClassroom,
      deleteOneFun: deleteOneClassroom,
      validator: ClassroomValidator,
      queryForm: {
        buildId: null,
        name: ''
      },
      form: {
        buildId: null,
        name: '',
        number: null,
        status: null
      },
      name: '教室',
      buildList: []
    }
  },
  mixins: [
    List
  ],
  mounted() {
    getBuildSimpleList({}).then(res => {
      const data = []
      for(let i of res.data) {
        data.push({
          id: i.id,
          label: i.name
        })
      }
      this.buildList = data
    })
  },
  methods: {
    onClickDept(data) {
      this.queryForm.buildId = data.id
      this.onClickSearch()
    }
  },
  computed: {
    ...mapGetters(['enums'])
  }
}
</script>

<style scoped>

</style>
